<?php
	/* function.inc.php module forum */
	
	spl_autoload_register("GetCategorieForum");
	spl_autoload_register("GetTopicItem");
	spl_autoload_register("LastPostedBy");
	spl_autoload_register("GetResponseByTopic");
	spl_autoload_register("GetUserIdFromPost");
	spl_autoload_register("GetLastUserIdFromForum");
	spl_autoload_register("GetAuthorPost");
	spl_autoload_register("GetPostEdit");
	spl_autoload_register("GetNbPostForumUser");
	spl_autoload_register("executeForumAutodelete");
	
	function GetCategorieForum()
	{
		// cette fonction retourne et va sur les categories du forum
		global $pdo;
		$cat = $pdo->query("SELECT * FROM forum_categorie JOIN forum_forum ON cat_id=forum_cat_id");

		$str = '
		<script>
			function goThis(location)
			{
				document.location.href="forum-voirtopic.html?fid="+location+"&num=1";
			}
		
		</script>
		<select name="movetopic" onchange="goThis(this.value);">'."\n";

			while($categorie = $cat->fetch(PDO::FETCH_OBJ)){

				if($c!=$categorie->cat_id)
				{
					if($optg==true)
						$str .= '</optgroup>'."\n";
					$str .= "\n".'<optgroup label="'.$categorie->cat_nom.'">'."\n";	
					$optg=true;
					$c=$categorie->cat_id;
				}
				$str .= '<option value="'.$categorie->forum_id.'" >'.$categorie->forum_name.'</option>';
			}
			$str .= '</optgroup></select>';
			
			return $str;
	}

	// function GetTopicItem prend les donn�es du topic

	// id

	// return the topic line

	function GetTopicItem($id,$item)

	{

		global $pdo;

		$q = $pdo->prepare("SELECT * FROM forum_topic WHERE topic_id=:id");

		$q->bindParam(":id",$id);

		$q->execute();

		

		$res = $q->fetch();

		return $res[$item];

	}

	//function LastPostedBy permet de r�cuperer les coordonn�es du dernier post

	//author : zulad

	//Int : $id

	//Return Str

	function LastPostedBy($id=0, $memberId=1)

	{

		global $pdo;



		$sql = "SELECT * FROM forum_post WHERE topic_id=" . intval($id) . " ORDER BY post_time DESC LIMIT 1";

		$q = $pdo->query($sql);

		

		$res = $q->fetch(PDO::FETCH_OBJ);

		$q->closeCursor();

		

		$pseudo = sex2color(id2sex($res->forum_membre_id),$res->forum_membre_id);

		

		return "par " . $pseudo . "<br />".format_temps($res->post_time);

	}

	

	//function GetResponseForTopic permet de r�cuperer les reponses du topic id

	//author : zulad

	//Int : $id

	//Return Str

	function GetResponseByTopic($id=0)

	{

		global $pdo;

		$sql = "SELECT * FROM forum_post WHERE topic_id=" . $id . " ORDER BY post_time DESC";

		//

		if($query=$pdo->query($sql))

			return $query;

	

		return null;

	}

	

	//function GetUserIdFromPost permet de r�cuperer l'id du membre qui a post�

	//author : zulad

	//Int : $id, $forumId

	//Return Str

	function GetUserIdFromPost($id=0, $forumId)

	{

		global $pdo;

		$q = $pdo->prepare("SELECT membre_id,membre_pseudo FROM forum_membres JOIN forum_post ON forum_membre_id=membre_id WHERE post_forum_id=".$forumId." AND post_id=".$id);

		$q->bindParam(":id",$id);

		$q->execute();

		

		$res = $q->fetch();

		$q->closeCursor();

		

		return $res["membre_id"]?$res["membre_id"]:null;

	}

	

	//function GetLastUserIdFromForum permet de r�cuperer l'id du membre qui a post�

	//author : zulad

	//Int : $forumId

	//Return Str

	function GetLastUserIdFromForum($forumId)

	{

		global $pdo;

		

		$sql = "SELECT forum_membre_id FROM forum_post WHERE post_forum_id=:id ORDER BY post_time DESC LIMIT 1";

		$q = $pdo->prepare($sql);

		$q->bindParam(":id",$forumId);

		$q->execute();

		

		$res = $q->fetch();

		$q->closeCursor();

		

		return $res["forum_membre_id"]?$res["forum_membre_id"]:null;

	}
	
	//function GetAuthorPost permet de retrouver l'auteur du post
	//memberId, postId 
	//Return $str ou Aucune information

	function GetAuthorPost($memberId,$postId)
	{

		return null;
	}

	

	//function CheckPostEdit permet de retrouver l'edition du post

	//postEdit, postId 

	//Return $str ou Aucune information

	

	function GetPostEdit($postEdit,$postId)

	{

	

		return null;

	}
	
	function GetNbPostForumUser($id){
		global $pdo;
		$id = intval($id);
		 $Forum = $pdo->prepare("SELECT * FROM forum_post");
   	  $Forum->execute();
	  $nbForum = array();
	  	while($data = $Forum->fetch(PDO::FETCH_OBJ)){
			if(isset($nbForum[$data->forum_membre_id]))
				$nbForum[$data->forum_membre_id]++;
			else
				$nbForum[$data->forum_membre_id] = 1;
		}
		return (isset($nbForum[$id]) ? $nbForum[$id] : 0);
	}
	
	function executeForumAutodelete(){

	global $pdo;

 if(date("d") == '01'){

				$post = $pdo->query("SELECT forum_post.topic_id as topic_id,forum_topic.forum_id as forum_id FROM forum_forum 

JOIN forum_topic ON forum_topic.forum_id = forum_forum.forum_id

JOIN forum_post ON forum_post.topic_id=forum_topic.topic_id 

WHERE forum_forum.forum_autodelete=1");



			

			while($data = $post->fetch()){

				$del = $query->prepare("DELETE forum_topic WHERE forum_id=?");

				$del->execute(array($data['forum_id']));

				

				$del = $query->prepare("DELETE forum_post WHERE topic_id=?");

				$del->execute(array($data['topic_id']));

			}

	}

}




?>